package com.kaili.basic.controller.web.v1;

import com.kaili.basic.pojo.dto.*;
import com.kaili.basic.pojo.vo.*;
import com.kaili.common.exception.RestException;
import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.service.IReportFormService;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.util.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.UUID;

@RestController
@ApiVersion(1)
@RequestMapping("/{api_version}")
@Validated
@Api(tags = "报表相关", description = "报表相关API")
public class ReportFormController {
    @Autowired
    private IReportFormService reportFormService;

    @GetMapping("report/inventoryCapacity")
    @ApiOperation(value = "库存容量")
    Page<InventoryCapacityVo> inventoryCapacity(@ModelAttribute InventoryCapacityDto inventoryCapacityDto){
        return reportFormService.inventoryCapacity(new Page<>(inventoryCapacityDto.getPage(),inventoryCapacityDto.getPageSize()),inventoryCapacityDto.getStoreId());
    }

    @GetMapping("report/inventoryCapacity/export")
    @ApiOperation(value = "库存容量导出")
    public void exportInventoryCapacity(HttpServletResponse response, @ModelAttribute InventoryCapacityDto inventoryCapacityDto) {
        List<InventoryCapacityVo> list = reportFormService.exportInventoryCapacity(inventoryCapacityDto);
        try {
            ExcelUtil.exportExcel(list,"库存容量","sheet1", InventoryCapacityVo.class, UUID.randomUUID().toString()+".xls",response);
        } catch (RestException e) {
            e.printStackTrace();
        }

    }

    @GetMapping("report/inventoryWarning")
    @ApiOperation(value = "库存预警")
    Page<InventoryWarningVo> inventoryWarning(@ModelAttribute InventoryWarningDto inventoryWarningDto){
        return reportFormService.inventoryWarning(new Page<>(inventoryWarningDto.getPage(),inventoryWarningDto.getPageSize()),inventoryWarningDto);
    }

    @GetMapping("report/inventoryWarning/export")
    @ApiOperation(value = "库存预警导出")
    public void exportInventoryWarning(HttpServletResponse response, @ModelAttribute InventoryWarningDto inventoryWarningDto) {
        List<InventoryWarningVo> list = reportFormService.exportInventoryWarning(inventoryWarningDto);
        try {
            ExcelUtil.exportExcel(list,"库存预警","sheet1", InventoryWarningVo.class, UUID.randomUUID().toString()+".xls",response);
        } catch (RestException e) {
            e.printStackTrace();
        }

    }

    @GetMapping("report/turnoverRate")
    @ApiOperation(value = "周转率")
    Page<TurnoverRateVo> turnoverRate(@ModelAttribute TurnoverRateDto turnoverRateDto){
        return reportFormService.turnoverRate(new Page<>(turnoverRateDto.getPage(),turnoverRateDto.getPageSize()),turnoverRateDto);
    }

    @GetMapping("report/turnoverRate/export")
    @ApiOperation(value = "周转率导出")
    public void exportTurnoverRate(HttpServletResponse response, @ModelAttribute TurnoverRateDto turnoverRateDto) {
        List<TurnoverRateVo> list = reportFormService.exportTurnoverRate(turnoverRateDto);
        try {
            ExcelUtil.exportExcel(list,"周转率","sheet1", TurnoverRateVo.class, UUID.randomUUID().toString()+".xls",response);
        } catch (RestException e) {
            e.printStackTrace();
        }

    }

    @GetMapping("report/leaseCost")
    @ApiOperation(value = "租赁费用")
    Page<LeaseCostVo> leaseCost(@ModelAttribute LeaseCostDto leaseCostDto){
        return reportFormService.leaseCost(new Page<>(leaseCostDto.getPage(),leaseCostDto.getPageSize()),leaseCostDto);
    }

    @GetMapping("report/leaseCost/export")
    @ApiOperation(value = "租赁费用导出")
    public void exportLeaseCost(HttpServletResponse response, @ModelAttribute LeaseCostDto leaseCostDto) {
        List<LeaseCostVo> list = reportFormService.exportLeaseCost(leaseCostDto);
        try {
            ExcelUtil.exportExcel(list,"租赁费用","sheet1", LeaseCostVo.class, UUID.randomUUID().toString()+".xls",response);
        } catch (RestException e) {
            e.printStackTrace();
        }
    }

    @GetMapping("report/inventoryLedger")
    @ApiOperation(value = "库存台账")
    Page<InventoryLedgerVo> inventoryLedger(@ModelAttribute InventoryLedgerDto inventoryLedgerDto){
        return reportFormService.inventoryLedger(new Page<>(inventoryLedgerDto.getPage(),inventoryLedgerDto.getPageSize()),inventoryLedgerDto);
    }

    @GetMapping("report/inventoryLedger/export")
    @ApiOperation(value = "库存台账导出")
    public void exportInventoryLedger(HttpServletResponse response, @ModelAttribute InventoryLedgerDto inventoryLedgerDto) {
        List<InventoryLedgerVo> list = reportFormService.exportInventoryLedger(inventoryLedgerDto);
        try {
            ExcelUtil.exportExcel(list,"库存台账","sheet1", InventoryLedgerVo.class, UUID.randomUUID().toString()+".xls",response);
        } catch (RestException e) {
            e.printStackTrace();
        }
    }

    @GetMapping("report/beginAndEnd")
    @ApiOperation(value = "期初期末")
    Page<BeginAndEndVo> beginAndEnd(@ModelAttribute BeginAndEndDto beginAndEndDto){
        return reportFormService.beginAndEnd(new Page<>(beginAndEndDto.getPage(),beginAndEndDto.getPageSize()),beginAndEndDto);
    }

    @GetMapping("report/beginAndEnd/export")
    @ApiOperation(value = "期初期末")
    public void exportBeginAndEnd(HttpServletResponse response, @ModelAttribute BeginAndEndDto beginAndEndDto) {
        List<BeginAndEndVo> list = reportFormService.exportBeginAndEnd(beginAndEndDto);
        try {
            ExcelUtil.exportExcel(list,"期初期末","sheet1", BeginAndEndVo.class, UUID.randomUUID().toString()+".xls",response);
        } catch (RestException e) {
            e.printStackTrace();
        }
    }

    @GetMapping("report/outOfStock")
    @ApiOperation(value = "可出库存")
    Page<OutOfStockVo> outOfStock(@ModelAttribute OutOfStockDto outOfStockDto){
        return reportFormService.outOfStock(new Page<>(outOfStockDto.getPage(),outOfStockDto.getPageSize()),outOfStockDto);
    }

    @GetMapping("report/outOfStock/export")
    @ApiOperation(value = "可出库存")
    public void exportOutOfStock(HttpServletResponse response, @ModelAttribute OutOfStockDto outOfStockDto) {
        List<OutOfStockVo> list = reportFormService.exportOutOfStock(outOfStockDto);
        try {
            ExcelUtil.exportExcel(list,"可出库存","sheet1", OutOfStockVo.class, UUID.randomUUID().toString()+".xls",response);
        } catch (RestException e) {
            e.printStackTrace();
        }
    }

    @GetMapping("report/inventory")
    @ApiOperation(value = "库存清单")
    Page<InventoryVo> inventory(@ModelAttribute InventoryDto inventoryDto){
        return reportFormService.inventory(new Page<>(inventoryDto.getPage(),inventoryDto.getPageSize()),inventoryDto);
    }

    @GetMapping("report/inventory/export")
    @ApiOperation(value = "库存清单")
    public void exportInventory(HttpServletResponse response, @ModelAttribute InventoryDto inventoryDto) {
        List<InventoryVo> list = reportFormService.exportInventory(inventoryDto);
        try {
            ExcelUtil.exportExcel(list,"库存清单","sheet1", InventoryVo.class, UUID.randomUUID().toString()+".xls",response);
        } catch (RestException e) {
            e.printStackTrace();
        }
    }
}
